package com.afei.bottomtabbar;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends BaseActivity {
    private EditText accountEt,passwordEt;
    private Button loginBtn;
    private String account = "daluzy";
    private String password = "111111";
    @Override
    public void initView() {
        contentView(R.layout.q_activity_login_page);
        setTitle(getResources().getString(R.string.login));
        setRightText1(getResources().getString(R.string.register), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,RegisterActivity.class);
                startActivityForResult(intent,1);

            }
        });
        accountEt = findViewById(R.id.account_et);
        passwordEt = findViewById(R.id.password_et);
        loginBtn = findViewById(R.id.login_btn);
        accountEt.setText(account);
        passwordEt.setText(password);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account2 = accountEt.getText().toString().trim();
                if (account2.isEmpty()){
                    Toast.makeText(context,getResources().getString(R.string.input_account), Toast.LENGTH_SHORT).show();
                    return;
                }
                String password2 = passwordEt.getText().toString().trim();
                if (password2.isEmpty()){
                    Toast.makeText(context,getResources().getString(R.string.input_password), Toast.LENGTH_SHORT).show();
                    return;
                }

                if (account.equals(account2) && password.equals(password2)){
//                    Intent intent = new Intent(context,SecondActivity.class);
                    Intent intent = new Intent(context,MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(context,getResources().getString(R.string.login_success), Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(context,getResources().getString(R.string.login_fail), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1){
            if (data != null) {
                account = data.getStringExtra("account");
                password = data.getStringExtra("password");
            }

        }
    }
}
